﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=System"
                    xmlns:toms="urn:TomsToolbox"
                    xmlns:dgx="urn:tom-englert.de/DataGridExtensions"
                    xmlns:styles="clr-namespace:tomenglertde.ResXManager.Styles">

  <Style x:Key="DataGridColumnHeaderThumbStyle" TargetType="{x:Type Thumb}">
    <Setter Property="Width" Value="4" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="Cursor" Value="SizeWE" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type Thumb}">
          <Border Padding="{TemplateBinding Control.Padding}" Background="{TemplateBinding Control.Background}" />
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style x:Key="DataGridRowHeaderThumbStyle" TargetType="{x:Type Thumb}">
    <Setter Property="Height" Value="4" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="Cursor" Value="SizeNS" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type Thumb}">
          <Border Padding="{TemplateBinding Control.Padding}" Background="{TemplateBinding Control.Background}" />
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style x:Key="DataGridColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="BorderThickness" Value="0,0,1,1" />
    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static styles:ResourceKeys.BorderBrush}}" />
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
    <Setter Property="Padding" Value="5,2" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
          <Grid>
            <Border x:Name="Border"
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding Control.BorderThickness}"
                    Padding="{TemplateBinding Control.Padding}">
              <ContentPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"
                                VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
                                HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                                RecognizesAccessKey="True" />
            </Border>
            <Path x:Name="SortDirectionVisual" Data="M0,0 L8,0 L4,4 Z" Fill="{TemplateBinding BorderBrush}" HorizontalAlignment="Center" VerticalAlignment="Top" Visibility="Hidden" />
            <Thumb x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" Style="{StaticResource DataGridColumnHeaderThumbStyle}" />
            <Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" Style="{StaticResource DataGridColumnHeaderThumbStyle}" />
          </Grid>
          <ControlTemplate.Triggers>
            <MultiTrigger>
              <MultiTrigger.Conditions>
                <Condition Property="CanUserSort" Value="True" />
                <Condition Property="SortDirection" Value="{x:Static componentModel:ListSortDirection.Ascending}" />
              </MultiTrigger.Conditions>
              <Setter TargetName="SortDirectionVisual" Property="Visibility" Value="Visible" />
            </MultiTrigger>
            <MultiTrigger>
              <MultiTrigger.Conditions>
                <Condition Property="CanUserSort" Value="True" />
                <Condition Property="SortDirection" Value="{x:Static componentModel:ListSortDirection.Descending}" />
              </MultiTrigger.Conditions>
              <Setter TargetName="SortDirectionVisual" Property="Visibility" Value="Visible" />
              <Setter TargetName="SortDirectionVisual" Property="LayoutTransform">
                <Setter.Value>
                  <RotateTransform Angle="180" />
                </Setter.Value>
              </Setter>
            </MultiTrigger>
            <MultiTrigger>
              <MultiTrigger.Conditions>
                <Condition Property="CanUserSort" Value="True" />
                <Condition Property="IsMouseOver" Value="True" />
              </MultiTrigger.Conditions>
              <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" />
            </MultiTrigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style x:Key="{x:Static styles:ResourceKeys.DataGridColumnHeaderStyle}" BasedOn="{StaticResource DataGridColumnHeaderStyle}" />

  <Style x:Key="DataGridRowHeaderStyle" TargetType="{x:Type DataGridRowHeader}">
    <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="BorderThickness" Value="0,0,1,1" />
    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static styles:ResourceKeys.BorderBrush}}" />
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
    <Setter Property="Padding" Value="2,0" />
    <Setter Property="MinWidth" Value="10" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type DataGridRowHeader}">
          <Grid>
            <Border x:Name="Border"
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding Control.BorderThickness}"
                    Padding="{TemplateBinding Control.Padding}">
              <StackPanel Orientation="Horizontal">
                <ContentPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" VerticalAlignment="Center" RecognizesAccessKey="True" />
                <Control SnapsToDevicePixels="False"
                         Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, Path=(Validation.HasError), Converter={x:Static toms:BooleanToVisibilityConverter.Default}}"
                         Template="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, Path=ValidationErrorTemplate}" />
              </StackPanel>
              <Border.Style>
                <Style TargetType="{x:Type Border}">
                  <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, Path=(Validation.HasError)}" Value="True">
                      <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent}" />
                    </DataTrigger>
                  </Style.Triggers>
                </Style>
              </Border.Style>
            </Border>
            <Thumb x:Name="PART_TopHeaderGripper" VerticalAlignment="Top" Style="{StaticResource DataGridRowHeaderThumbStyle}" />
            <Thumb x:Name="PART_BottomHeaderGripper" VerticalAlignment="Bottom" Style="{StaticResource DataGridRowHeaderThumbStyle}" />
          </Grid>
          <ControlTemplate.Triggers>
            <Trigger Property="IsRowSelected" Value="True">
              <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
            </Trigger>
            <MultiTrigger>
              <MultiTrigger.Conditions>
                <Condition Property="IsRowSelected" Value="True" />
                <Condition Property="Selector.IsSelectionActive" Value="False" />
              </MultiTrigger.Conditions>
              <Setter Property="Control.Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
            </MultiTrigger>
            <Trigger Property="IsMouseOver" Value="True">
              <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}" />
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style x:Key="{x:Static styles:ResourceKeys.DataGridRowHeaderStyle}" BasedOn="{StaticResource DataGridRowHeaderStyle}" />

  <Style x:Key="DataGridSelectAllButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static styles:ResourceKeys.BorderBrush}}" />
    <Setter Property="BorderThickness" Value="0,0,1,1" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
          <Grid>
            <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">
              <Polygon x:Name="Arrow" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="3" Fill="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" Stretch="Uniform" Points="0,10 10,10 10,0" />
            </Border>
          </Grid>
          <ControlTemplate.Triggers>
            <Trigger Property="UIElement.IsMouseOver" Value="True">
              <Setter TargetName="Arrow" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" Property="Shape.Stroke" />
            </Trigger>
            <Trigger Property="ButtonBase.IsPressed" Value="True">
              <Setter TargetName="Arrow" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" Property="Shape.Fill" />
            </Trigger>
            <Trigger Property="UIElement.IsEnabled" Value="False">
              <Setter TargetName="Arrow" Property="UIElement.Visibility" Value="Collapsed" />
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style x:Key="{x:Static styles:ResourceKeys.DataGridCellStyle}" TargetType="{x:Type DataGridCell}">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderBrush" Value="Transparent" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type DataGridCell}">
          <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
                  Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
            <ContentPresenter Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                              VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}" />
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
    <Style.Triggers>
      <Trigger Property="IsSelected" Value="True">
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
        <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
      </Trigger>
      <Trigger Property="IsKeyboardFocusWithin" Value="True">
        <Setter Property="BorderBrush" Value="{DynamicResource {x:Static DataGrid.FocusBorderBrushKey}}" />
      </Trigger>
    </Style.Triggers>
  </Style>

  <Style x:Key="{x:Static styles:ResourceKeys.DataGridRowStyle}" TargetType="DataGridRow">
    <Style.Triggers>
      <Trigger Property="IsSelected" Value="True">
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
      </Trigger>
      <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}, Path=SelectionUnit}" Value="Cell">
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
      </DataTrigger>
      <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}, Path=SelectionUnit}" Value="CellOrRowHeader">
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
      </DataTrigger>
    </Style.Triggers>
  </Style>

  <Style x:Key="{x:Static styles:ResourceKeys.DataGridStyle}" TargetType="{x:Type DataGrid}">
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static styles:ResourceKeys.BorderBrush}}" />
    <Setter Property="VerticalContentAlignment" Value="Stretch" />
    <Setter Property="HorizontalGridLinesBrush" Value="{DynamicResource {x:Static styles:ResourceKeys.BorderBrush}}" />
    <Setter Property="VerticalGridLinesBrush" Value="{DynamicResource {x:Static styles:ResourceKeys.BorderBrush}}" />
    <Setter Property="RowHeaderStyle" Value="{StaticResource DataGridRowHeaderStyle}" />
    <Setter Property="ColumnHeaderStyle" Value="{StaticResource DataGridColumnHeaderStyle}" />
    <Setter Property="dgx:Tools.ApplyInitialSorting" Value="True" />
    <Setter Property="dgx:ColumnStyles.DefaultColumnStyles">
      <Setter.Value>
        <dgx:DataGridColumnStyleCollection>
          <dgx:DataGridColumnStyle ColumnType="DataGridComboBoxColumn"
                                   ElementStyle="{DynamicResource {x:Static styles:ResourceKeys.DataGridComboBoxElementStyle}}"
                                   EditingElementStyle="{DynamicResource {x:Static styles:ResourceKeys.DataGridComboBoxEditingElementStyle}}" />
          <dgx:DataGridColumnStyle ColumnType="DataGridCheckBoxColumn"
                                   ElementStyle="{DynamicResource {x:Static styles:ResourceKeys.DataGridCheckBoxElementStyle}}"
                                   EditingElementStyle="{DynamicResource {x:Static styles:ResourceKeys.DataGridCheckBoxEditingElementStyle}}" />
        </dgx:DataGridColumnStyleCollection>
      </Setter.Value>
    </Setter>
    <Setter Property="RowValidationErrorTemplate">
      <Setter.Value>
        <ControlTemplate>
          <TextBlock Margin="2,0,0,0" VerticalAlignment="Center" Foreground="Red" Text="!" FontWeight="Bold" />
        </ControlTemplate>
      </Setter.Value>
    </Setter>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type DataGrid}">
          <Border Background="{TemplateBinding Control.Background}"
                  BorderBrush="{TemplateBinding Control.BorderBrush}"
                  BorderThickness="{TemplateBinding Control.BorderThickness}" SnapsToDevicePixels="True"
                  Padding="{TemplateBinding Control.Padding}">
            <ScrollViewer Name="DG_ScrollViewer" Focusable="False">
              <Control.Template>
                <ControlTemplate TargetType="{x:Type ScrollViewer}">
                  <Grid>
                    <Grid.RowDefinitions>
                      <RowDefinition Height="Auto" />
                      <RowDefinition Height="*" />
                      <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                      <ColumnDefinition Width="Auto" />
                      <ColumnDefinition Width="*" />
                      <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Button Command="{x:Static DataGrid.SelectAllCommand}" Focusable="False"
                            Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=CellsPanelHorizontalOffset}"
                            Style="{StaticResource DataGridSelectAllButtonStyle}"
                            Visibility="{Binding Path=HeadersVisibility, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, ConverterParameter={x:Static DataGridHeadersVisibility.All}}" />
                    <DataGridColumnHeadersPresenter Name="PART_ColumnHeadersPresenter" Grid.Row="0" Grid.Column="1"
                                                    Visibility="{Binding Path=HeadersVisibility, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, ConverterParameter={x:Static DataGridHeadersVisibility.Column}}" />
                    <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
                                            CanContentScroll="{TemplateBinding ScrollViewer.CanContentScroll}" />
                    <Border Grid.Row="0" Grid.Column="2" BorderBrush="{DynamicResource {x:Static styles:ResourceKeys.BorderBrush}}" BorderThickness="0,0,0,1" />
                    <ScrollBar Name="PART_VerticalScrollBar" Grid.Row="1" Grid.Column="2" Orientation="Vertical"
                               Maximum="{TemplateBinding ScrollViewer.ScrollableHeight}"
                               ViewportSize="{TemplateBinding ScrollViewer.ViewportHeight}"
                               Visibility="{TemplateBinding ScrollViewer.ComputedVerticalScrollBarVisibility}"
                               Value="{Binding Path=VerticalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}" />
                    <Grid Row="2" Column="1">
                      <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=NonFrozenColumnsViewportHorizontalOffset}" />
                        <ColumnDefinition Width="*" />
                      </Grid.ColumnDefinitions>
                      <ScrollBar Name="PART_HorizontalScrollBar" Grid.Column="1" Orientation="Horizontal"
                                 Maximum="{TemplateBinding ScrollViewer.ScrollableWidth}"
                                 ViewportSize="{TemplateBinding ScrollViewer.ViewportWidth}"
                                 Visibility="{TemplateBinding ScrollViewer.ComputedHorizontalScrollBarVisibility}"
                                 Value="{Binding Path=HorizontalOffset, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}" />
                    </Grid>
                  </Grid>
                </ControlTemplate>
              </Control.Template>
              <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
            </ScrollViewer>
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
    <Style.Triggers>
      <MultiTrigger>
        <MultiTrigger.Conditions>
          <Condition Property="ItemsControl.IsGrouping" Value="True"/>
          <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="False"/>
        </MultiTrigger.Conditions>
        <Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
      </MultiTrigger>
    </Style.Triggers>
  </Style>

</ResourceDictionary>